/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */
package cn.com.ksplatform.modules.system.auth.cachemanager.util;

import java.util.HashMap;
import java.util.Map;

import cn.com.ksplatform.core.context.PlatformContext;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
/**
 * 缓存工具类
 * ClassName:CacheUtil.java <br/>
 * Date: 2015年12月14日<br/>
 * 
 * @author Mr.Xiao
 * @version 1.0
 */
public class CacheUtil {
	
	static CacheManager cacheManager;
	
	static Map<String, Cache> caches = new HashMap<String, Cache>();
	
	static{
		if(cacheManager == null){
			cacheManager = PlatformContext.getBean(CacheManager.class);
		}
	}
	public static Object getObject(String cacheName,String key){
		if(!caches.containsKey(cacheName)){
			Cache cache = cacheManager.getCache(cacheName);
			if(cache != null){
				caches.put(cacheName, cache);
			}
		}
		
		Element rtn = caches.get(cacheName).get(key);
		if(rtn != null){
			return rtn.getObjectValue();
		}
		return null;
	}
	public static <T> T getObject(String cacheName,String key,Class<T> requiredType){
		return (T) getObject(cacheName,key);
	}
	
}
